package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdQualityRewardsMessageDao;
import com.ccp.dev.qbdms.dao.WQbdQualityRewardsMsgSubDao;
import com.ccp.dev.qbdms.model.WQbdQualityRewardsMessage;
import com.ccp.dev.qbdms.model.WQbdQualityRewardsMsgSub;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 质量奖惩管理 信息操作服务类
 *
 * @author hw
 * @date 2020-07-06 14:10:36
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdQualityRewardsMessageService extends BaseService<WQbdQualityRewardsMessage> {

    @Resource
    private WQbdQualityRewardsMessageDao dao;
    @Resource
    private WQbdQualityRewardsMsgSubService wQbdQualityRewardsMsgSubService;
    @Resource
    private WQbdQualityRewardsMsgSubDao wQbdQualityRewardsMsgSubDao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WQbdQualityRewardsMessage> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdQualityRewardsMessage data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    /**
     * 重写add方法,新增
     * @param wQbdQualityRewardsMessage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(WQbdQualityRewardsMessage wQbdQualityRewardsMessage){

        wQbdQualityRewardsMessage.setCompanyName(wQbdQualityRewardsMessage.getFilldept());

        wQbdQualityRewardsMsgSubService.addAndUpdate(wQbdQualityRewardsMessage.getwQbdQualityRewardsMsgSubList(),wQbdQualityRewardsMessage.getId());
        return super.add(wQbdQualityRewardsMessage);
    }

    /**
     * 重写update方法 编辑
     * @param wQbdQualityRewardsMessage
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(WQbdQualityRewardsMessage wQbdQualityRewardsMessage){
        wQbdQualityRewardsMsgSubService.addAndUpdate(wQbdQualityRewardsMessage.getwQbdQualityRewardsMsgSubList(),wQbdQualityRewardsMessage.getId());
        return super.update(wQbdQualityRewardsMessage);
    }

    /**
     * 批量删除数据信息
     * @param arrayId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delByIdArray(String[] arrayId){
        wQbdQualityRewardsMsgSubService.delByRefIds(arrayId);
        return super.delByIdArray(arrayId);
    }

    /**
     * 导出用查询
     * @param queryFilter 查询参数
     * @return list
     */
    public List<WQbdQualityRewardsMessage> queryAll(QueryFilter queryFilter){
        return dao.queryAll(queryFilter);
    }

    /**
     * 导入新增
     * @param list  数据集
     * @throws Exception 异常
     */
    public void addImportData(List<WQbdQualityRewardsMessage> list) throws Exception{
        List<WQbdQualityRewardsMsgSub> subData = new ArrayList<>();
        String path = BaseUtil.getCurrentTenOrgCodePath();
        Date date = new Date();
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        String userId = sysUser.getUserId();
        String tenantId = ContextUtil.getCurrentUserTenantId();
        SysOrg sysOrg = (SysOrg) ContextUtil.getCurrentOrg();
        String code = sysOrg.getCode();
        String orgName = sysOrg.getOrgName();
        String fillMan = sysUser.getFullName();
        String account = sysUser.getAccount();
        list.forEach(a->{
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            a.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            ZhiLiangConst.addCommonFields(a);
            a.setCompanyName(a.getFilldept());
            if(StringUtil.isEmpty(a.getBelongdeptname())){
                a.setBelongdeptname(path);
            }
            final BigDecimal[] bigDecimal = {new BigDecimal(0)};
            List<WQbdQualityRewardsMsgSub> wQbdQualityRewardsMsgSubs = a.getwQbdQualityRewardsMsgSubList();
            if(null != wQbdQualityRewardsMsgSubs && wQbdQualityRewardsMsgSubs.size()>0){
                wQbdQualityRewardsMsgSubs.forEach(b->{
                    b.setRefid(mainId);
                    b.setId(ProjectUUIDUtils.getUUIDFor32());
                    b.setCreateTime(date);
                    b.setCreateBy(userId);
                    b.setTenantId(tenantId);
                    if(BeanUtils.isNotEmpty(b.getMoney())){
                        bigDecimal[0] = bigDecimal[0].add(b.getMoney());
                    }else{
                        bigDecimal[0] = bigDecimal[0].add(new BigDecimal(0));
                    }
                });
                subData.addAll(wQbdQualityRewardsMsgSubs);
            }
            a.setDataFrom(ZhiLiangConst.RESTS);
            a.setAggregateAmount(bigDecimal[0]);
        });

        if(!list.isEmpty()){
            dao.addList(list);
            if(!subData.isEmpty()){
                wQbdQualityRewardsMsgSubService.addList(subData);
            }
        }
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdQualityRewardsMessage getParentAndChilden(String id) {
        return dao.getParentAndChilden(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdQualityRewardsMessageList 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WQbdQualityRewardsMessage> wQbdQualityRewardsMessageList,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdQualityRewardsMessageList,extendSysFileList);
        List<WQbdQualityRewardsMessage> rewardsMessageList = new ArrayList<>();
        for(WQbdQualityRewardsMessage wQbdQualityRewardsMessage : wQbdQualityRewardsMessageList){
            if(StringUtil.isEmpty(wQbdQualityRewardsMessage.getFilldeptHospitalId())){
                wQbdQualityRewardsMessage.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdQualityRewardsMessage.getFilldeptid()));
            }else{
                wQbdQualityRewardsMessage.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdQualityRewardsMessage.getFilldeptHospitalId()));
            }
            // 直接插入子表数据
            wQbdQualityRewardsMsgSubDao.addList(wQbdQualityRewardsMessage.getwQbdQualityRewardsMsgSubList());
            // 直接插主表数据
            rewardsMessageList.add(wQbdQualityRewardsMessage);
        }
        return super.addList(rewardsMessageList);
    }

}
